#include "FKRoomInterface.h"

#include <QQuickItem>
#include <QQmlContext>

#include "FKVisualInterfaceView.h"

#include "FKLogger.h"
#include "FKModelRole.h"
#include "FKEventNames.h"

#include "FKActorsModel.h"

const int FKRoomInterface::_maxChatLines=20;

FKRoomInterface::FKRoomInterface(QObject *parent):FKVisualInterface(parent){
    FK_CBEGIN
    _actorsModel=0;
    FK_CEND
}

FKRoomInterface::~FKRoomInterface(){
    FK_DBEGIN
    FK_DEND
}

void FKRoomInterface::setupDefaults(){
    FKVisualInterface::setupDefaults();
    QQuickItem* vi=view()->rootObject();
    connect(vi,FKQmlSignal1(chatMessage),FKQmlSlot1(chatMessageRequest));
    connect(vi,SIGNAL(quitRequested()),SIGNAL(quitRoomRequested()));
    connect(vi,SIGNAL(startRequested()),SIGNAL(startRoomRequested()));
    connect(vi,FKQmlSignal2(areaClicked),FKQmlSlot2(areaClickRequest));
    connect(this,SIGNAL(clearChatRequest()),vi,SIGNAL(eClearChatHistory()));
    connect(this,FKQmlSignal1(addChatMessageRequest),vi,FKQmlSignal1(eAddChatMessage));
    connect(this,FKQmlSignal2(startRoomLoadingRequest),vi,FKQmlSignal2(eStartLoading));
    connect(this,FKQmlSignal2(setRoomLoadingProgressRequest),vi,FKQmlSignal2(eSetLoadingProgress));
    connect(this,FKQmlSignal2(addRoomLoadingProgressRequest),vi,FKQmlSignal2(eAddLoadingProgress));
    connect(this,SIGNAL(startRoomRequest()),vi,SIGNAL(eRoomStarted()));
    connect(this,FKQmlSignal2(setActorsOptions),vi,FKQmlSignal2(eSetActorsOptions));
}

void FKRoomInterface::setupContextProperties(QQmlContext *context){
    FKVisualInterface::setupContextProperties(context);
//    context->setContextProperty("actorsModel",_actorsModel);
}

FKVisualInterfaceView* FKRoomInterface::initialization(QWindow *parent){
    FKVisualInterfaceView* v=new FKVisualInterfaceView(QString("FKVisualInterfaces/FKRoomInterface.qml"),parent);
    if(parent)v->resize(parent->size());
    v->setClearBeforeRendering(false);
    return v;
}

void FKRoomInterface::chatMessageRequest(FKQmlArgument msg){
    emit chatMessageRequested(msg.toString());
}

void FKRoomInterface::areaClickRequest(FKQmlArgument x, FKQmlArgument y){
    emit areaClicked(QPoint(x.toInt(),y.toInt()));
}

void FKRoomInterface::addChatMessage(QString msg){
    _chatHistory.append(msg);
    if(_chatHistory.count()>_maxChatLines)_chatHistory.removeFirst();
    emit clearChatRequest();
    foreach (QString s, _chatHistory) {
        emit addChatMessageRequest(FKQmlArgument(s));
    }
}

void FKRoomInterface::clearChat(){
    emit clearChatRequest();
}

void FKRoomInterface::setActorsModel(FKActorsModel *model){
    if(model==_actorsModel)return;
    _actorsModel=model;
    view()->rootContext()->setContextProperty("actorsModel",model);
//    emit setActorsOptions(1,QVariant(_actorsItemModel->horizontalHeaderItem(1)->data(FKModelRole::actorOptions)));
}

void FKRoomInterface::roomStarted(){
    emit startRoomRequest();
}

void FKRoomInterface::roomLoadingBegan(int val1, int val2){
    emit startRoomLoadingRequest(FKQmlArgument(val1),FKQmlArgument(val2));
}

void FKRoomInterface::setRoomLoadingProgress(int val1, int val2){
    emit setRoomLoadingProgressRequest(FKQmlArgument(val1),FKQmlArgument(val2));
}

void FKRoomInterface::addRoomLoadingProgress(int val1, int val2){
    emit addRoomLoadingProgressRequest(FKQmlArgument(val1),FKQmlArgument(val2));
}

void FKRoomInterface::mouseClicked(QPoint pos, qint32 obj){
    Q_UNUSED(pos)
    if(obj==-1)return;
    emit worldAction(obj,FKEventName::a_buttonClick,QVariant(),actorsModel()->actorForUserID(activeUser()));
}
